package com.CGFrame.struts.bean.project;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.configuration.Configuration;

import com.CGFrame.struts.POJOs.Elementgroup;
import com.CGFrame.struts.POJOs.Projectconfig;
import com.CGFrame.struts.POJOs.Shotgroup;
import com.CGFrame.struts.bean.ProjectOperate;
import com.CGFrame.struts.bean.ProjectOperateFactory;
import com.config.GenericConfig;

public class ProjectAddFile {
	
	/**获取根目录*/
	private String filebase = GenericConfig.getInstance().getFileBase();
	
	public boolean addShotgroup(String[] groupName, Shotgroup sgp,
			String basePath, String rootPath) throws Exception {

		String projectId = String.valueOf(sgp.getProject().getProjectId());
		ProPathConst ppc = new ProPathConst();
		String path = null;

		if (sgp.getShotGroupName().equals(ppc.getParam3())) {
			path = getEpisPath(rootPath, basePath, projectId, "Epis", null);
		} else {
			path = getEpisPath(rootPath, basePath, projectId, sgp.getShotGroupName(),
					"esp");
		}
		if (path == null) {
			System.out.println("创建镜头组目录路径为空，创建失败");
			return false;
		}
		for (int i = 0; i < groupName.length; i++) {
			mkdictory(path + groupName[i]);
		}
		return true;
	}

	public boolean addShot(String[] name, Shotgroup sgp, String basePath)
			throws Exception {
		String projectId = String.valueOf(sgp.getProject().getProjectId());
		String path = null;
		path = getShotPath(basePath, projectId, sgp.getShotgroup()
				.getShotGroupName(), sgp.getShotGroupName());
		String fs = System.getProperties().getProperty("file.separator");
		ProPathConst ppc = new ProPathConst();
		System.out.println(path);
		if (path == null) {
			System.out.println("创建镜头目录路径为空，创建失败");
			return false;
		}

		for (int i = 0; i < name.length; i++) {
			mkdictory(path + name[i]);

		}
		for (int i = 0; i < name.length; i++) {
			if (!name[i].trim().equals("")) {
				mkdictory(path + name[i] + fs + ppc.getParam10() + fs
						+ ppc.getParam11());
				mkdictory(path + name[i] + fs + ppc.getParam10() + fs
						+ ppc.getParam12());
			}
		}
		return true;
	}

	private File mkdictory(String fileName) {
		File file = new File(fileName);
		if (!file.exists()) {
			file.mkdirs();
		}
		return file;
	}

	public String getEpisPath(String rootPath, String basePath, String projectId,
			String episName, String espName) throws Exception {
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		String fs = System.getProperties().getProperty("file.separator");
		String rPath =rootPath+fs+"project"+fs+"Shot";
        FoldCopy fc = new FoldCopy();
	
		ProPathConst ppc = new ProPathConst();
		StringBuffer bf = new StringBuffer();
		Projectconfig pcof = (Projectconfig) pop.UniqueQuery("Projectconfig",
				"projectId," + projectId);
		bf.append(basePath);
		if (basePath.indexOf(fs) == -1) {
			bf.append(fs);
		}
		if (pcof == null) {
			System.out.println("数据库不存在工程配置信息");
			return null;
		}
		// Project
		bf.append(pcof.getProjectName()).append(fs).append(ppc.getParam1());
		// scenes
		bf.append(fs).append(ppc.getParam2()).append(fs);
		// Episodes
		bf.append(ppc.getParam3()).append(fs);
        
		if (episName.equals("Epis")) {
			if (pcof.getHasEspCheck() == 1) {
				return bf.toString();
			} else {
				//Shot下的Pre_Production 拷贝到Episodes下面  每个Production对应一个Pre_Production
				fc.copyDirectiory(rPath, bf.toString());
				bf.append(ppc.getParam4()).append(fs);
				return bf.toString();
			}
		}

		if (espName.equals("esp")){
			bf.append(episName).append(fs);
			//Shot下的Pre_Production 拷贝到Episodes下面  每个Production对应一个Pre_Production
			fc.copyDirectiory(rPath, bf.toString());
			bf.append(ppc.getParam4()).append(fs);
			return bf.toString();
		} else {
			System.out.println("场下面不能新增镜头组");
			return null;
		}

	}

	public String getShotPath(String basePath, String projectId,
			String espName, String sceName) {
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();

		String fs = System.getProperties().getProperty("file.separator");
		ProPathConst ppc = new ProPathConst();
		StringBuffer bf = new StringBuffer();
		Projectconfig pcof = (Projectconfig) pop.UniqueQuery("Projectconfig",
				"projectId," + projectId);
		bf.append(basePath);
		if (basePath.indexOf(fs) == -1) {
			bf.append(fs);
		}
		if (pcof == null) {
			System.out.println("数据库不存在工程配置信息");
			return null;
		}
		// Project
		bf.append(pcof.getProjectName()).append(fs).append(ppc.getParam1());
		// scenes
		bf.append(fs).append(ppc.getParam2()).append(fs);
		// Episodes
		bf.append(ppc.getParam3()).append(fs);
		if ((pcof.getHasEspCheck() == 0) && (pcof.getHasScenceCheck() == 0)) {
			return bf.toString();
		}

		if (pcof.getHasEspCheck() == 1) {
			bf.append(espName).append(fs);
		}
		bf.append(ppc.getParam4()).append(fs);
		if ((pcof.getHasScenceCheck() == 1) && (pcof.getIsSingleShot() == 1)) {
			bf.append(sceName).append(fs);
		} else {
			bf.append(ppc.getParam5()).append(fs);
		}
		return bf.toString();

	}

	public boolean addElementGroup(String[] groupName, Elementgroup egp,
			String filePath, String rootPath) {
		String fs = System.getProperties().getProperty("file.separator");
		String projectId = String.valueOf(egp.getProject().getProjectId());
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		
		String rPath = rootPath+fs+"project"+fs+"Element";

		List lst = new ArrayList();
		System.out.println("111" + egp.getElementGroupName());
		while (!egp.getElementGroupName().equals("Assets")) {
			lst.add(egp.getElementGroupName());
			egp = egp.getElementgroup();
		}

		ProPathConst ppc = new ProPathConst();
		StringBuffer bf = new StringBuffer();
		bf.append(filePath);
		if (filePath.indexOf(fs) == -1) {
			bf.append(fs);
		}
		Projectconfig pcof = (Projectconfig) pop.UniqueQuery("Projectconfig",
				"projectId," + projectId);
		// Project
		bf.append(pcof.getProjectName()).append(fs).append(ppc.getParam1());
		// scenes
		bf.append(fs).append(ppc.getParam2()).append(fs);
		bf.append(egp.getElementGroupName()).append(fs);
		bf.append(ppc.getParam4()).append(fs);
		for (int i = 0; i < lst.size(); i++) {
			bf.append(lst.get(lst.size() - i - 1));
			bf.append(fs);
		}
		System.out.println(bf);
		for (int j = 0; j < groupName.length-1; j++) {
			mkdictory(bf.toString() + groupName[j]);
		}
		return true;
	}

	public boolean addElement(String[] name, Elementgroup egp) {
		if (name == null || egp==null ) return true;
		String fs = System.getProperties().getProperty("file.separator");
		String projectId = String.valueOf(egp.getProject().getProjectId());
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();

		List lst = new ArrayList();
		while (!egp.getElementGroupName().equals("Assets")) {
			lst.add(egp.getElementGroupName());
			egp = egp.getElementgroup();
		}

		ProPathConst ppc = new ProPathConst();
		StringBuffer bf = new StringBuffer();
		bf.append(filebase);
		if (filebase.indexOf(fs) == -1) {
			bf.append(fs);
		}
		Projectconfig pcof = (Projectconfig) pop.UniqueQuery("Projectconfig",
				"projectId," + projectId);
		// Project
		bf.append(pcof.getProjectName()).append(fs).append(ppc.getParam1());
		// scenes
		bf.append(fs).append(ppc.getParam2()).append(fs);
		bf.append(egp.getElementGroupName()).append(fs);
		bf.append(ppc.getParam4()).append(fs);
		for (int i = 0; i < lst.size(); i++) {
			bf.append(lst.get(lst.size() - i-1));
			bf.append(fs);
		}
		
		for (int j = 0; j < name.length; j++) {
			if( name[j]==null || name[j].equals("")) continue;  // not null
			mkdictory(bf.toString() + name[j]+fs+ppc.getParam10()+fs+ppc.getParam11());
			mkdictory(bf.toString() + name[j]+fs+ppc.getParam10()+fs+ppc.getParam12());
		}
		return true;
	}
}
